package com.struts.web.user.manage;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.base.Constants;
import com.ibatis.po.user.TUserInfo;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CLGAction;
import com.lg.system.CSqlMap;

public class ForgetPassAction extends CLGAction {

	
	   public  ActionForward getQuestion(ActionMapping actionMapping,
	            ActionForm actionForm,
	            HttpServletRequest request,
	            HttpServletResponse response) throws Exception{
		   
		   SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		   ActionMessages msgs = new ActionMessages();
		   ForgetPassForm aWebForm = (ForgetPassForm)actionForm;
		   TUserInfo user = new TUserInfo();
		   user.setLoginName(aWebForm.getName());
		   ActionForward forward = null;
		   String question  = "";
		   try{
		   question = (String)sqlmap.queryForObject("T_USER_INFO.selectForQuestion",user);
		   if(question!="" && question!=null){
			   aWebForm.setQuestion(question);
			   aWebForm.setName(user.getLoginName());
			   
			   forward = actionMapping.findForward("setpass");
		   }else{
			   msgs.add("commonMsgs",new ActionMessage(Constants.NOT_SET_PASS));
			   saveErrors(request, msgs);
			   forward = actionMapping.findForward("fail");
		   }
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   return forward;
	   } 
	   
       public  ActionForward setPass(ActionMapping actionMapping,
               ActionForm actionForm,
               HttpServletRequest request,
               HttpServletResponse response){
    	   ActionForward forward = null;
    	   ActionMessages msgs = new ActionMessages();
		   try{
		   
		   SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		   ForgetPassForm aWebForm = (ForgetPassForm)actionForm;
		   TUserInfo user = new TUserInfo();
		   TUserInfo user1 = new TUserInfo();
		   user.setLoginName(aWebForm.getName());
		   user.setAnswer(aWebForm.getAnswer());
		   user.setPassword(aWebForm.getPassword());
		   int count = 0;
		   count = ((Integer)sqlmap.queryForObject("T_USER_INFO.selectCountByQuestion",user)).intValue();
		   if(count>0){
			  
			   sqlmap.update("T_USER_INFO.setPass",user);
			   user1 = (TUserInfo)sqlmap.queryForObject("T_USER_INFO.selectByLoginName",user);
			   request.getSession().setAttribute("TUser", user1);
			   forward = actionMapping.findForward("userLoginSuccess");
		   }else{
			   msgs.add("commonMsgs",new ActionMessage(Constants.WRONG_ANSWER));
			   saveErrors(request, msgs);
			   forward = actionMapping.findForward("fail");
		   }
    	   }catch(Exception e){
    		   e.printStackTrace();
    	   }
    	   
		return forward;  
	   }
}
